export function request(url, data, options = {}) {

    let defaultOptions =
        {showLoading: true, mask: false, method: "GET",title:'正在加载'};

    defaultOptions = {
        ...defaultOptions,
        ...options,
    }

    let header = {}
    if(defaultOptions.method == "POST"){
        header['content-type'] = "application/x-www-form-urlencoded"
    }
    if (defaultOptions.showLoading) {
        wx.showLoading({
            title: defaultOptions.title,
            mask: defaultOptions.mask
        })
    }
    console.log(defaultOptions)
    return new Promise((resolve, reject) => {
        wx.request({
            url,
            data,
            method:defaultOptions.method,
            header,
            success: function (res) {
                if (res.statusCode == 200) {
                    resolve(res.data)
                } else {
                    reject("网络错误")
                }
            },
            fail: function (error) {
                reject(error)
            },
            complete: function () {
                if (defaultOptions.showLoading) {
                    wx.hideLoading();
                }
            }
        })
    })

}



